package com.company.mediaplayer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;


import java.io.File;
import java.util.ArrayList;

public class Index extends AppCompatActivity {

    //播放器状态参数
    public static final int PLAY = 1;
    public static final int PAUSE = 2;
    public static final int NEXT =3;
    public static final int PREVIOUS =4;


    //各类widgets
    private MediaPlayer mp;
    private Button choose,play,pause,previous,next ;
    private SeekBar skb;
    private TextView tv1,tv2;

    //下拉列表
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    public static String[] mfilename;
    private int songnum;//歌曲序号
    private static File file ;//MP3 文件的文件夹


    private Intent intent ;

    public Index(){
        file =new File(MusicService.MUSIC_PATH);

        mfilename= file.list(new MusicFilter());
        for (int i=0 ;i<file.list(new MusicFilter()).length;i++){
            Log.i("msg",mfilename[i]);
        }
    }
    public static String[] getMfilename(){
        file =new File(MusicService.MUSIC_PATH);
        mfilename= file.list(new MusicFilter());
        return mfilename;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        //各类Widgets初始化
        play = (Button)findViewById(R.id.play);
        pause = (Button)findViewById(R.id.pause);
        next = (Button)findViewById(R.id.next);
        previous = (Button)findViewById(R.id.previous);
        skb = (SeekBar)findViewById(R.id.skb);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
        spinner = (Spinner)findViewById(R.id.spinner);

        intent =new Intent("com.company.mediaplayer.musicservice");






        //下拉列表
        //将可选内容与ArrayAdapter连接
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mfilename);
        //设置下拉列表风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter添加到spinner中
        spinner.setAdapter(adapter);
        //添加Spinner事件监听
        spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                songnum = arg2;
                //设置显示当前选择的项
                arg0.setVisibility(View.VISIBLE);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                spinner.setSelection(songnum,true);
            }
        });



        //监听
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pause(v);
            }
        });

        play.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                play(v);
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                next(v);
                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        spinner.setSelection(songnum,true);
                    }
                });

            }
        });

        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                previous(v);
            }
        });

        skb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser ==true){
                    mp.seekTo(progress);
                    tv1.setText(toTime(progress));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
    }



    //各类Button


    public void play(View view) {
        intent.putExtra("music", PLAY);
        intent.putExtra("songnum", songnum);

        Log.i("msg", "music start.....");
        startService(intent);

        bindService(intent, new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                MusicService.Mbinder ms = (MusicService.Mbinder) service;
                mp = ms.getMbinder();
                tv1.setText(toTime(mp.getCurrentPosition()));
                tv2.setText(toTime(mp.getDuration()));
                skb.setMax(mp.getDuration());
                startSeekBar();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }

        }, BIND_AUTO_CREATE);

    }

    public void pause(View view) {
        intent.putExtra("music", PAUSE);
        Log.i("msg", "music pause.....");
        startService(intent);
    }

    public void previous(final View view) {
        intent.putExtra("music", PREVIOUS);
        if(songnum-1<=0)
            songnum=0;
        else
            songnum--;
        intent.putExtra("songnum", songnum);
        Log.i("msg", "music previous.....");
        startService(intent);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
                next(view);//歌曲播放完毕,自动播放下一首.
            }
        });
    }

    public void next(View view) {
        intent.putExtra("music", NEXT);
        if(songnum+1>=mfilename.length-1)
            songnum=mfilename.length-1;
        else
            songnum++;
        intent.putExtra("songnum", songnum);
        Log.i("msg", "music next.....");
        startService(intent);
    }

    public void select(View view){

    };

    //线程
    public  Handler mhandler = new Handler();
    public void startSeekBar(){

        mhandler.post(r);
    }

    Runnable r =new Runnable() {
        @Override
        public void run() {
            int currentPosition = mp.getCurrentPosition();
            tv1.setText(toTime(currentPosition));
            skb.setProgress(currentPosition);
            mhandler.postDelayed(r, 300);
        }
    };



    //时间转换
    public  String toTime(int currentPosition){
        int minute = currentPosition/(1000*60);
        int second = (currentPosition/1000)%60;
        String XXXX = minute+":"+second;
        return XXXX;
    }

    public void end(View view) {
        stopService(intent);
    }
}
